package com.bj.jiufu.bsns.sys.service.impl;

import com.bj.jiufu.base.BaseService;
import com.bj.jiufu.base.RespParam;
import com.bj.jiufu.bsns.sys.dao.mapping.AppVersionMapper;
import com.bj.jiufu.bsns.sys.dao.mapping.LoginAuthMapper;
import com.bj.jiufu.bsns.sys.dao.mapping.UserMapper;
import com.bj.jiufu.bsns.sys.entity.AppVersion;
import com.bj.jiufu.bsns.sys.entity.LoginAuth;
import com.bj.jiufu.bsns.sys.entity.User;
import com.bj.jiufu.bsns.sys.service.SysService;
import com.bj.jiufu.util.base.UuidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.resources.Messages_it;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by susen-pc on 2016/9/1.
 */
@Transactional  /*事务注解必须加*/
public class SysServiceImpl extends BaseService implements SysService {

    @Resource(name = "userMapper")
    private UserMapper userMapper;

    @Resource(name = "appVersionMapper")
    private AppVersionMapper appVersionMapper;

    @Resource(name = "loginAuthMapper")
    private LoginAuthMapper loginAuthMapper;


    @Override
    public RespParam regiester(User user) {
        try {
            //查找手机是否已经存在
            User hasUser = userMapper.selectByMobile(user.getMobile());
            if (null != hasUser) {
                return setErrorMsg("手机号已经注册");
            }
            user.setUserId(UuidUtil.get32UUID());
            user.setCreateTime(new Date());
            userMapper.insert(user);
            return setRetData(user.getUserId());
        } catch (Exception e) {
            return setErrorMsg(e);
        }
    }

    @Override
    public RespParam getCurrAppVersion() {
        try {
            AppVersion currAppVersion = appVersionMapper.getCurrAppVersion();
            return setRetData(currAppVersion);
        } catch (Exception e) {
            return setErrorMsg(e.getMessage());
        }
    }

    @Override
    public RespParam getCaptcha(String mobile) {
        try {
            LoginAuth captcha = loginAuthMapper.getCaptcha(mobile);
            //时间验证
            Date createTime = captcha.getCreateTime();
            Date now = new Date();
            long gap = now.getTime() - createTime.getTime();
            System.out.println(gap / 1000 + " s");
            return setRetData(captcha);
        } catch (Exception e) {
            return setErrorMsg(e.getMessage());
        }
    }


    @Override
    public RespParam sendCaptcha(String mobile) {
        try {
            LoginAuth auth = new LoginAuth();
            auth.setCreateTime(new Date());
            auth.setMobile(mobile);
            //FIXME:短信网关，发送验证码，并且入库
            auth.setCaptcha("11");
            loginAuthMapper.insert(auth);
            return setSuccess();
        } catch (Exception e) {
            return setErrorMsg(e);
        }
    }

    @Override
    public RespParam setNewPassword(String mobile, String newPassword) {
        try {
            User user = userMapper.selectByMobile(mobile);
            user.setPassword(newPassword);
            user.setUpdateTime(new Date());
            userMapper.updateByPrimaryKeySelective(user);
            return setSuccess();
        } catch (Exception e) {
            return setErrorMsg(e);
        }
    }

    @Override
    public RespParam login(String mobile, String md5_password) {
        try {
            User user = userMapper.selectByMobile(mobile);
            if (user.getPassword().equals(md5_password)) {
                return setSuccess();
            } else {
                return setErrorMsg("密码有误");
            }
        } catch (Exception e) {
            return setErrorMsg(e);
        }
    }

}
